import {Component, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {ParentService} from "@train/parent/parent.service";
import {
  SNSResult, StudentTestLastScoreVO, StudentTestLastScoreWrapVO, TrainAgeDictionarie, TrainStudent,
  TrainTestCategoryVO
} from "@interface/index";
import {AuthService} from "@shared/service/auth/auth.service";
import {ActivatedRoute, Params} from "@angular/router";
import {Content} from "@shared/components/toolbar/toolbar-content";
import {ImageServeService} from "@shared/service/image/image-serve.service";

@Component({
  selector: 'seed-view-test-scores',
  templateUrl: './view-test-scores.component.html',
  styleUrls: ['./view-test-scores.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class ViewTestScoresComponent implements OnInit {

  public categoryList: Array<StudentTestLastScoreWrapVO> = [];
  public curStudentScore : StudentTestLastScoreVO;
  public defaultIcon;
  public ageList: Array<TrainAgeDictionarie> = [];
  public curAge: TrainAgeDictionarie;
  public authOrgId: string;
  public studentInfo: TrainStudent;
  public paramObj = {
    studentId: 0,
    testDate: 0,
    testModeId: 0,
    testMethodId: 0
  };
  @ViewChild('tipsContent') tipsContent;
  @ViewChild(Content) content;
  constructor(
    private parentService: ParentService,
    private authService: AuthService,
    private activatedRoute: ActivatedRoute,
    private imageServe: ImageServeService,
  ) {
    this.defaultIcon = this.imageServe.getUserIcon();
    this.authOrgId = authService.getAuthOrgId();
    this.defaultIcon = this.imageServe.getUserIcon();
    activatedRoute.params.subscribe((param: Params) => {
      this.paramObj.studentId = param['studentId'];
      this.paramObj.testDate = param['testDate'];
      this.paramObj.testModeId = param['testModeId'];
      this.paramObj.testMethodId = param['testMethodId'];
      this.getStudentInfo();
    });
  }

  getIcon(name: string): string {
    return this.imageServe.getIcon(name);
  }

  /**
   * @author guoqs1331
   * @time 2018/4/16
   * @description 学生信息
   */
  getStudentInfo() {
    const param = {
      studentId: this.paramObj.studentId,
      orgUserId: this.authOrgId
    };
    this.parentService.findStudentInfo(param).subscribe(res => {
      this.studentInfo = res['data'];
      this.getLastScoreGroup();
    })
  }

  /**
   * @author guoqs1331
   * @time 2018/4/16
   * @description getAgeDicts
   */
  async getAgeDicts(): Promise<SNSResult<TrainAgeDictionarie[]>> {
    const result = await this.parentService.getAgeDicts().toPromise();
    this.ageList = result['data'];
    // this.curAge = this.ageList[0];
    this.ageList.forEach(item =>{
      if(item.id === this.studentInfo.testAgeId){
        this.curAge = item;
      }
    });
    return result;
    // return Promise.resolve(result);
  }

  /**
   * @author guoqs1331
   * @time 2018/4/15
   * @description 以组为单位获取最后一次成绩
   */
  async getLastScoreGroup() {
    await this.getAgeDicts();
    this.requestItemScoreInfo(this.curAge.id);
  }

  /**
   * @author guoqs1331
   * @time 2018/4/16
   * @description 以组为单位获取最后一次成绩
   */
  requestItemScoreInfo(curAgeId: number) {
    const param = {
      ageDictId: curAgeId,
      studentId: this.paramObj.studentId,
      sportType: 3002 // 暂时写死（篮球类型）
    };
    this.parentService.getLastScoreGroup(param).subscribe(res => {
      if (res && res['data']) {
        this.categoryList = res['data'];
        this.curStudentScore = this.categoryList[0].lastScore[0];
      }
    });
  }

  /**
   * @author guoqs1331
   * @time 2018/4/17
   * @description 根据时间戳获取年和月份和日期
   */
  getMonthAndDay(dateStr) {
    const date = new Date(dateStr);
    console.log(date.getMonth());
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
  }

  /**
   * @author guoqs1331
   * @time 2018/4/13
   * @description
   */
  showTips() {
    this.tipsContent.show();
  }

  /**
   * @author guoqs1331
   * @time 2018/4/13
   * @description
   */
  closeTips() {
    this.tipsContent.hide();
  }

  /**
   * @author guoqs1331
   * @time 2018/4/16
   * @description 选择年龄组
   */
  changeSelect(e) {
    this.ageList.forEach(item => {
      if(item.id === +e){
        this.curAge = item;
      }
    });
    this.requestItemScoreInfo(e);
  }

  ngOnInit() {
  }
}
